//
//  CRLoginView.m
//  CRChatRoom
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CRLoginView.h"
#import "CRLoginModel.h"

@interface CRLoginView () <UITextFieldDelegate>


@end

@implementation CRLoginView

static  CGFloat headY = 110;

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupSubView];
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"login_bg"]];
    }
    return self;
}
- (void)setupSubView
{
    
    CGFloat hight = CGRectGetHeight([UIScreen mainScreen].bounds);
    if (hight == 480) {
        headY = 60;
    }
    
    CGFloat margin = 20;
    CGFloat contH = 30;
    CGFloat contW = 200;
    CGFloat contX = (self.width - 200) / 2;
    CGFloat headH = 75;
    CGFloat headW = 75;

    self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, headW, headH)];
    [self.headImageView layoutIfNeeded];
    [self.headImageView.layer setMasksToBounds:YES];
    [self.headImageView.layer setCornerRadius:self.headImageView.frame.size.width / 2.0];
    self.headImageView.centerX = self.centerX;
    
    
    self.userText = [[UITextField alloc] initWithFrame:CGRectMake(contX, headY + headH + margin + 25, contW, contH)];
    self.userText.delegate =self;
    
    self.headImageView.center = self.userText.center;
    self.headImageView.y = headY;
    self.userText.textAlignment = NSTextAlignmentCenter;

    self.passwordText = [[UITextField alloc] initWithFrame:CGRectMake(contX, self.userText.y + contH + margin, contW, contH)];
    self.passwordText.delegate = self;
    
    self.passwordText.secureTextEntry = YES;
    self.passwordText.textAlignment = NSTextAlignmentCenter;

    self.loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.loginButton.layer.borderWidth = 0.1;
    [self.loginButton.layer setMasksToBounds:YES];
    [self.loginButton.layer setCornerRadius:self.headImageView.frame.size.width / 13];

    self.loginButton.frame = CGRectMake(contX, self.passwordText.y + contH + margin + 20, contW, contH);
    
    
    self.rememberPButton = [[UIButton alloc] init];
    self.rememberPButton.frame = CGRectMake(contX - 10, self.loginButton.y + margin * 2, 100, contH);
    [self.rememberPButton setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    [self.rememberPButton setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
    [self.rememberPButton setTitle:@"记住密码" forState:UIControlStateNormal];
    [self.rememberPButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.rememberPButton.titleLabel.font = [UIFont systemFontOfSize:14];

    
    self.autoLButton = [[UIButton alloc] init];
    self.autoLButton.frame = CGRectMake(contX + 100, self.loginButton.y  + margin * 2, 100, contH);
    [self.autoLButton setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    [self.autoLButton setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
    [self.autoLButton setTitle:@"自动登录" forState:UIControlStateNormal];
    self.autoLButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.autoLButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    self.forgetPButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.forgetPButton.frame = CGRectMake(20, self.height- contH - 15, 80, contH);
    
    self.setUserButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.setUserButton.frame = CGRectMake(self.width - 100, self.height- contH - 15, 80, contH);
    
    self.userText.placeholder = @"用户名";
    self.passwordText.placeholder = @"密码";
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.forgetPButton setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [self.setUserButton setTitle:@"新用户" forState:UIControlStateNormal];
    
    self.userText.borderStyle = UITextBorderStyleRoundedRect;
    self.passwordText.borderStyle = UITextBorderStyleRoundedRect;
    
    [self addSubview:self.headImageView];
    [self addSubview:self.userText];
    [self addSubview:self.passwordText];
    [self addSubview:self.loginButton];
    [self addSubview:self.forgetPButton];
    [self addSubview:self.setUserButton];
    [self addSubview:self.rememberPButton];
    [self addSubview:self.autoLButton];
}

- (void)setLoginModel:(CRLoginModel *)loginModel{
    if (_loginModel != loginModel) {
        _loginModel = nil;
        _loginModel = loginModel;
    }
    [self layoutModel];
}
- (void)layoutModel
{
    _headImageView.image = [UIImage imageNamed:@"tubiao"];
    _userText.text = _loginModel.userName;
    _passwordText.text = _loginModel.password;
    
}

#pragma mark >>>> TextField代理

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    CGFloat offset = self.height - (textField.frame.origin.y + textField.frame.size.height + 226 + 100);
    
    if (offset <= 0) {
        [UIView animateWithDuration:0.3 animations:^{
            
            CGRect frame = self.frame;
            frame.origin.y = offset;
            self.frame = frame;
            
        }];
    }
    return YES;
}


//按return按钮的操作
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    [UIView animateWithDuration:0.3 animations:^{
        
        CGRect frame = self.frame;
        
        frame.origin.y = 0.0;
        
        self.frame = frame;
        
    }];
    return YES;
}


@end
